package com.sskj.mine;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.hey.lib.HeySpinner;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.bean.ImgHighBean;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_AddPayment)
public class AddPaymentActivity extends BaseActivity<AddPaymentPresenter> {

    @BindView(R2.id.select_type)
    HeySpinner selectType;
    @BindView(R2.id.name_edt)
    EditText nameEdt;
    @BindView(R2.id.bank_name_edt)
    EditText bankNameEdt;
    @BindView(R2.id.bank_card_edt)
    EditText bankCardEdt;
    @BindView(R2.id.bank_card_edt2)
    EditText bankCardEdt2;
    @BindView(R2.id.bank_name_layout)
    LinearLayout bankNameLayout;
    @BindView(R2.id.account_text)
    TextView accountText;
    @BindView(R2.id.account_edt)
    EditText accountEdt;  // 支付宝账号
    @BindView(R2.id.pay_pwd)
    TextView payPwd;
    @BindView(R2.id.pay_pwd_edit)
    EditText payPwdEdit;
    @BindView(R2.id.select_img)
    ImageView selectImg;
    @BindView(R2.id.select_img_tip)
    TextView selectImgTip;
    @BindView(R2.id.select_img_layout)
    LinearLayout selectImgLayout;
    @BindView(R2.id.btn_submit)
    Button btnSubmit;

    private List<String> payTypes = new ArrayList<>();
    private int type = 0;
    private final int SELECT_PIC = 1000;
    private String path = "";
    @Autowired
    public String mType;
    @Autowired
    public String payType;

    private String url;
    private String name;  // 是哪种支付方式
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_add_payment;
    }

    @Override
    public AddPaymentPresenter getPresenter() {
        return new AddPaymentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);
        mType = getIntent().getStringExtra("mType");
        name = getIntent().getStringExtra("name");
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                nameEdt.setText(userData.getRealname());
            } else {
                userData = null;
            }
        });
        setTitle(App.INSTANCE.getString(R.string.mine_addPaymentActivity1_j));
        payTypes.add(App.INSTANCE.getString(R.string.mine_securityActivity39_j));
        payTypes.add(App.INSTANCE.getString(R.string.mine_securityActivity40_j));
        payTypes.add(App.INSTANCE.getString(R.string.mine_addPaymentActivity4_j));

        selectType.setOnSelectListener(position -> {
            type = position;
            switch (type) {
                case 0:
                    setText(accountText, App.INSTANCE.getString(R.string.mine_addPaymentActivity5_j));
                    accountEdt.setHint(App.INSTANCE.getString(R.string.mine_addPaymentActivity6_j));
                    selectImgTip.setText(App.INSTANCE.getString(R.string.mine_addPaymentActivity7_j));
                    selectImgLayout.setVisibility(View.VISIBLE);
                    bankNameLayout.setVisibility(View.GONE);
                    accountText.setVisibility(View.VISIBLE);
                    accountEdt.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    setText(accountText, App.INSTANCE.getString(R.string.mine_addPaymentActivity8_j));
                    accountEdt.setHint(App.INSTANCE.getString(R.string.mine_addPaymentActivity9_j));
                    selectImgTip.setText(App.INSTANCE.getString(R.string.mine_addPaymentActivity7_j));
                    selectImgLayout.setVisibility(View.VISIBLE);
                    bankNameLayout.setVisibility(View.GONE);
                    accountText.setVisibility(View.VISIBLE);
                    accountEdt.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    accountText.setVisibility(View.GONE);
                    accountEdt.setVisibility(View.GONE);
                    selectImgLayout.setVisibility(View.GONE);
                    bankNameLayout.setVisibility(View.VISIBLE);
                    break;
            }
        });
        selectType.attachData(payTypes);
        selectType.setSelectIndex("wx".equals(name) ? 0 : "alipay".equals(name) ? 1 : 2);
        if (mType.equals("edit")) {
            selectType.setEnabled(false);
        }
    }

    @Override
    public void initData() {

    }

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent() {
        ClickUtil.click(selectImg, () -> {
            new RxPermissions(this)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                    .subscribe(granted -> {
                        if (granted) {
                            PictureSelector.create(this)
                                    .openGallery(PictureMimeType.ofImage())
                                    .previewImage(true)
                                    .isCamera(true)
                                    .imageFormat(PictureMimeType.JPEG)
                                    .maxSelectNum(1)
                                    .compress(true)
                                    .forResult(SELECT_PIC);
                        }
                    });
        });
        ClickUtil.click(btnSubmit, () -> {
            String payName = "";
            String bankName = bankNameEdt.getText().toString().trim();  // 银行开户行
            String bankNum = bankCardEdt.getText().toString().trim();  // 银行卡号
            String account = accountEdt.getText().toString().trim();// 支付宝账号 微信
            String payPwd = payPwdEdit.getText().toString().trim();  // 资金密码
            switch (type) {
                case 0:
                    if (TextUtils.isEmpty(account)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityActivity43_j));
                        return;
                    }
                    if (TextUtils.isEmpty(url)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addPaymentActivity11_j));
                        return;
                    }
                    payName = "wx";
                    break;
                case 1:
                    if (TextUtils.isEmpty(account)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityActivity45_j));
                        return;
                    }
                    if (TextUtils.isEmpty(url)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addPaymentActivity11_j));
                        return;
                    }
                    payName = "alipay";
                    break;
                case 2:
                    if (TextUtils.isEmpty(bankName)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addPaymentActivity13_j));
                        return;
                    }
                    if (isEmpty(bankCardEdt)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityActivity35_j));
                        return;
                    }
                    if (isEmpty(bankCardEdt2)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addPaymentActivity15_j));
                        return;
                    }
                    if (!getText(bankCardEdt).equals(bankCardEdt2.getText().toString())) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_addPaymentActivity16_j));
                        return;
                    }

                    if (TextUtils.isEmpty(bankNum)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityActivity41_j));
                        return;
                    }
                    if (bankNum.length() < 10 || bankNum.length() > 20) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_securityActivity42_j));
                        return;
                    }
                    payName = "backcard";
                    break;
            }
            if (TextUtils.isEmpty(payPwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_shellActivity5_j));
                return;
            }
            mPresenter.requestAddPayWay(mType, payName, bankName, bankNum, account, url, payPwd);
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PIC) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                if (selectList != null && selectList.size() > 0) {
                    LocalMedia media = selectList.get(0);
                    String url;
                    if (media.isCompressed()) {
                        url = media.getCompressPath();
                    } else {
                        url = media.getPath();
                    }
                    path = url;
                    switch (requestCode) {
                        case SELECT_PIC:
                            path = url;
                            Glide.with(this).load(url).into(selectImg);
                            break;
                    }
                    mPresenter.submitVerify(new File(path));
                }
            }
        }
    }


    public static void start(Context context, String type, String name) {
        Intent intent = new Intent(context, AddPaymentActivity.class);
        intent.putExtra("mType", type);
        intent.putExtra("name", name);
        context.startActivity(intent);
    }

    public void updateUi(ImgHighBean imgHighBean) {
        url = imgHighBean.getUrl();
    }

}
